package com.skywolf.chem.utils.sms;

import com.alibaba.fastjson.JSONObject;
import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import com.skywolf.chem.common.enums.ResponseCodeEnum;
import com.skywolf.chem.common.sysconfig.exception.BizException;

/**
 * Created by pei_pei on 2020/6/28.
 */
public class SendSms {

    private static String accessKeyId;
    private static String accessSecret;

    public SendSms(String accessKeyId, String accessSecret) {
        this.accessKeyId = accessKeyId;
        this.accessSecret = accessSecret;
    }

    public static AliySmsResponse sendMsg(String phone,String code)throws BizException {
        DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou", accessKeyId, accessSecret);
        IAcsClient client = new DefaultAcsClient(profile);

        CommonRequest request = new CommonRequest();
        request.setSysMethod(MethodType.POST);
        request.setSysDomain("dysmsapi.aliyuncs.com");
        request.setSysVersion("2017-05-25");
        request.setSysAction("SendSms");
        request.putQueryParameter("RegionId", "cn-hangzhou");
        request.putQueryParameter("PhoneNumbers", phone);
        request.putQueryParameter("SignName", "川体在线");
        request.putQueryParameter("TemplateCode", "SMS_196615747");
        request.putQueryParameter("TemplateParam", "{\"code\":\""+code+"\"}");
        try {
            CommonResponse response = client.getCommonResponse(request);
            AliySmsResponse aliyunSmsResponse = JSONObject.parseObject(response.getData(), AliySmsResponse.class);
            return aliyunSmsResponse;
        } catch (ServerException e) {
            e.printStackTrace();
        } catch (ClientException e) {
            e.printStackTrace();
        }
        throw new BizException(ResponseCodeEnum.SENDMSG_ERROR);
    }
}
